<?php
namespace app\admin\model;



use think\Model;

class Gcategory extends Model
{
    protected $pk = 'cate_id';

    protected $error;
    public function getError()
    {
        return $this->error;
    }
    public static function getCateList($data=[]) //商品列表引入分类
    {

        //分类下拉框
        $cate_list=self::field(true)->select();

        //返回值
        return $cate_list;
    }
    public function add(array $data,array $allowField = [],bool $replace = false)
    {
        try{
            if(empty($data)){
                $this->error = '数据不能为空';
            }
            if(!empty($allowField)){
                $this->allowField($allowField);
            }
            $this->replace($replace)->save($data);
            $id = $this->toArray();
            return $id[$this->pk];
        }catch (\Exception $e){
            $this->error = $e->getMessage();
            return false;
        }
    }
    public static function getGcategory()
    {
        $model = self::field(true);

        $cate_list = $model->select();
        return $cate_list;
        
    }
    public static function getCate($id = 0, $query=[],$page = false)
    {
//        $model=self::alias('w')->field('cate_id,cate_name,parent_id,sort_order')
//            ->field("if((select cate_id from ecm_gcategory n where n.parent_id = w.cate_id limit 1),1,0) as has_next");
        $sql = self::alias('n')->field('cate_id')->whereColumn('n.parent_id','=','w.cate_id')->limit('1')->buildsql();
        return  self::alias('w')
        ->field('cate_id,cate_name,parent_id,sort_order')
        ->field("if(".$sql.",1,0) as has_next")
            ->where('w.parent_id',$id)->select();

//        if(isset($data['id'])&&''!==$data['id'])
//        {
//            $model->where('w.parent_id',$data['id']);
//        }
//        if(isset($data['id'])&&''!==$data['id'])
//        {
//            $model->where('w.parent_id',$data['id']);
//        }
//        if ($page){
//            $result=$model->select();
//        }else{
//            $result = $model->paginate([
//               'list_rows' =>10,
//                'query'    =>$query,
//            ]);
//        }



//        return $result;
    }






}